package cn.smbms.controller.provider;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.smbms.pojo.Provider;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.service.provider.ProviderServiceImpl;
import cn.smbms.tools.Constants;
import cn.smbms.tools.PageSupport;

@Controller
@RequestMapping("/provider")
public class ProviderController {
	
	@Resource(name="ProviderServiceImpl")
	ProviderService providerServiceImpl;
	
	@RequestMapping(value="providerList")
	public ModelAndView getProviderList(
			@RequestParam(value="queryProName",required=false)String queryProName,
			@RequestParam(value="queryProCode",required=false)String queryProCode,
			@RequestParam(value="pageIndex",required=false)String pageIndex,
			HttpServletRequest request
			){
		
		ModelAndView mav=new ModelAndView();
		
		List<Provider> providerList=null;
		

		
		//设置页面容量
		int pageSize=Constants.pageSize;
		
		//设置当前页码
		int currentPageNo=1;
		if(queryProName==null) {
			queryProName="";
		}
		
		if(queryProCode==null){
			queryProCode="";
		}
		
		if(pageIndex != null &&!"".equals(pageIndex)){
    		try{
    			currentPageNo = Integer.valueOf(pageIndex);
    		}catch(NumberFormatException e){
    			
    		}
    		
    	}	
		
		
		//总数量
		int totalCount=providerServiceImpl.getProviderCount(queryProName, queryProCode);
		
		//总页数
		PageSupport pages=new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		
		int totalPageCount = pages.getTotalPageCount(); 
		
		//控制首页和尾页
		if(currentPageNo < 1) {
			currentPageNo=1;
		}else if(currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		
		providerList=providerServiceImpl.getProviderList(queryProName, queryProCode,currentPageNo,pageSize);
		mav.addObject("providerList", providerList);
		
		mav.addObject("queryProName", queryProName);
		mav.addObject("queryProCode", queryProCode);
		mav.addObject("totalPageCount", totalPageCount);
		mav.addObject("totalCount", totalCount);
		mav.addObject("currentPageNo", currentPageNo);
		
		mav.setViewName("providerlist");
		return mav;
		
		
		
		
		
		
		
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
